#define _CRT_SECURE_NO_WARNINGS
#include"Slist.h"
// 动态申请一个节点
SListNode* BuySListNode(SLTDateType x)
{
	SListNode* tmp = (SListNode*)malloc(sizeof(SListNode));
	if (tmp == NULL) {
		perror("malloc failed");
		return NULL;
	}
	tmp->data = x;
	tmp->next = NULL;
	return tmp;
}
// 单链表打印
void SListPrint(SListNode* plist)
{
	SListNode* phead = plist;
	while (phead)
	{
		printf("%d->",phead->data);
		phead = phead->next;
	}
	printf("NULL\n");

}

// 单链表尾插
void SListPushBack(SListNode** pplist, SLTDateType x)
{
	SListNode* tmp = BuySListNode(x);
	if (*pplist == NULL)
	{
		*pplist = tmp;
		return;
	}
	SListNode* cur = *pplist;
	while (cur->next)
	{
		cur = cur->next;
	}
	cur->next = tmp;
	return;
}

// 单链表的头插
void SListPushFront(SListNode** pplist, SLTDateType x)
{
	SListNode* tmp = BuySListNode(x);
	tmp->next = *pplist;
	*pplist = tmp;

}

// 单链表的尾删
void SListPopBack(SListNode** pplist)
{
	if (*pplist == NULL)return;
	SListNode* tail = *pplist;
	if (tail->next == NULL)
	{
		free(tail->next);
		*pplist = NULL;
		return;
	}
	while (tail->next->next != NULL)
	{
		tail = tail->next;
	}
	free(tail->next);
	tail->next = NULL;
}

// 单链表头删
void SListPopFront(SListNode** pplist)
{
	if (*pplist == NULL)return;
	SListNode* plist = *pplist;
	*pplist = plist->next;
	free(plist);
	plist = NULL;
}

// 单链表查找
SListNode* SListFind(SListNode* plist, SLTDateType x)
{
	if (plist == NULL)return NULL;
	SListNode* cur = plist;
	while (cur->next!=NULL)
	{
		if (cur->data == x)return cur;
		cur = cur->next;
	}
	if (cur->next == NULL && cur->data != x)return NULL;
	return cur;
}
// 单链表在pos位置之后插入x
// 分析思考为什么不在pos位置之前插入？
void SListInsertAfter(SListNode* pos, SLTDateType x)
{
	if (pos == NULL)return ;
	SListNode* tmp = BuySListNode(x);
	tmp->next = pos->next;
	pos->next = tmp;
}
// 单链表删除pos位置之后的值
// 分析思考为什么不删除pos位置？
void SListEraseAfter(SListNode* pos)
{
	if (pos == NULL)return;
	SListNode* tmp = pos->next;
	pos->next = tmp->next;
	free(tmp);
}
// 单链表的销毁
void SListDestroy(SListNode** plist)
{
	SListNode* cur = *plist;
	while (cur != NULL)
	{
		SListNode* tmp = cur->next;
		free(cur);
		cur = tmp;
		free(tmp);
	}
	*plist = NULL;
}